<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	
	class mod_special extends _init_model
	{
		public $SPE_ID;
		public $SPE_SDATE;
		public $SPE_STIME;
		public $SPE_EDATE;
		public $SPE_ETIME;
		public $SPE_REMAIN;
		public $CAF_ID;
		
		function mod_special(){
			parent::__construct();
		
			$this->tableName = 'special';
		}
		
		function get_special_cafe($count=5){			
			$this->db->select('*');
			$this->db->join('cafe', 'cafe.CAF_ID = special.CAF_ID');
			$this->db->join('cafe_img_logo', 'cafe.CAF_IMG_L_ID = cafe_img_logo.CAF_IMG_L_ID', 'right');
			$this->db->order_by('SPE_ID', 'RANDOM');
			$this->db->limit($count);	
			$query = $this->db->get($this->tableName);			
			$return = $query->result_array();
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				
				for ($i=0; $i<count($return); $i++){
					if (empty($row['CAF_IMG_L_FILENAME']))
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
					else
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return[$i]['CAF_IMG_L_FILENAME'];
				}
			}
			
			return $return;			
		}
		
		function get_list_special_cafe()
			{
				$obj = new $this;
				$result = $this->mod_list_special_cafe->get();
				
				$return = array();
				
				if ($result->count() > 0)
				{
					$i = 0;
						
					foreach ($result as $row)
					{
					
						
						$str_start = date("Y-m-d");
						$chack_e_date = $row->SPE_EDATE;
		
						if ($chack_e_date != null) {
						$str_end = $row->SPE_EDATE;
							////////////end time day//////////////
						$str_start = strtotime($str_start); // ทำวันที่ให้อยู่ในรูปแบบ timestamp
						$str_end = strtotime($str_end); // ทำวันที่ให้อยู่ในรูปแบบ timestamp
			
						$nseconds = $str_end - $str_start; // วันที่ระหว่างเริ่มและสิ้นสุดมาลบกัน
						$ndays = round($nseconds / 86400); // หนึ่งวันมี 86400 วินาที
							////////////end time day//////////////
						}
							$return[$i] = array("SPE_SDATE" => $row->SPE_SDATE,
												"SPE_STIME" => $row->SPE_STIME,
												"CAF_ID" => $row->CAF_ID,
												"CAF_NAME" => $row->CAF_ID(true)->CAF_NAME,
												"SPE_EDATE" => $row->SPE_EDATE,
												"SPE_REMAIN" => $ndays);
							$i++;
							// 190 (true)->CAF_NAME
					}
				}
				return $return;
			}
		function get_cafe_list()
			{
				$this->load->model("local/data/cafe/mod_cafe");
				$result = $this->mod_cafe->get();
				
				$return = array();
				if ($result->count() > 0)
				{
					$i=0;
					foreach ($result as $row)
					{
					
						$return[$i] = array("CAF_ID" => $row->CAF_ID,
											"CAF_NAME" => $row->CAF_NAME,
											"CAF_FACEBOOK" => $row->CAF_FACEBOOK,
											"MEM_ID" => $row->MEM_ID,
											"MEM_FNAME" => $row->MEM_ID(true)->MEM_FNAME,
											"MEM_SNAME" => $row->MEM_ID(true)->MEM_SNAME,
											"CAF_T_TITLE" => $row->CAF_TITLE,
											"PAK_NAME" => $row->PAK_ID(true)->PAK_NAME,
											"CAF_STATUS" => $row->CAF_STATUS);
							
						$i++;
					}
				}
	
				return $return;
			}
	}
?>